package biz.ezcom.servlet.filter.servlet;

import java.io.IOException;
import java.io.ObjectInputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import biz.ezcom.servlet.filter.bean.User;

/**
 * request以流的方式直接转成对象,以http传输的对象及其所有的属性必需是Serializable
 * @author Felix
 */
public class ObjectServlet extends HttpServlet {
	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("ObjectServlet.doPost()====================");
		doParameter(request, response);
	}
	
	private void doParameter(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ObjectInputStream ois = new ObjectInputStream(request.getInputStream());
		try {
			User userA = (User) ois.readObject();
			System.out.println("id: " + userA.getId());
			System.out.println("username: " + userA.getUsername().getUsername());
			System.out.println("password: " + userA.getPassword());
			request.setAttribute("userA", userA);
			
			User userB = (User) ois.readObject();
			System.out.println("id: " + userB.getId());
			System.out.println("username: " + userB.getUsername().getUsername());
			System.out.println("password: " + userB.getPassword());
			request.setAttribute("userB", userB);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		request.getRequestDispatcher("/WEB-INF/jsp/ObjectServlet.jsp").forward(request, response);
	}
	
}
